﻿using Management.Auxiliary;
using Management.Core.SiliconCarbide;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class SiliconCarbideController : Controller
    {
        private SiliconCarbideManager scm = new SiliconCarbideManager();
        //
        // GET: /Control/SiliconCarbide/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="siliconCarbideId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(int? siliconCarbideId, int? pageNumber, int? pageSize, int? order)
        {
            Paging<SiliconCarbide> _pagingSiliconCarbide = new Paging<SiliconCarbide>();
            if (pageNumber != null && pageNumber > 0) _pagingSiliconCarbide.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingSiliconCarbide.PageSize = (int)pageSize;
            var _paging = scm.FindPageList(_pagingSiliconCarbide, siliconCarbideId, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            List<SelectListItem> _listItems = new List<SelectListItem>(2);
            _listItems.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems;
            return PartialView();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="_siliconCarbide"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]

        public ActionResult Add([Bind(Include = "SiliconCarbideID, Name, IsDisabled, Created_at, Updated_at")]SiliconCarbide _siliconCarbide)
        {
            _siliconCarbide.Created_at = DateTime.Now;
            _siliconCarbide.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                //检查名称
                if (scm.IsContains(u => u.Name == _siliconCarbide.Name))
                {
                    _resp.Code = 0;
                    _resp.Message = "类型已存在";
                }
                else
                {
                    _resp = scm.Add(_siliconCarbide);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="siliconCarbideid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int siliconCarbideid)
        {
            Response _resp = new Response();
            var _siliconCarbide = scm.Find(siliconCarbideid);
            if (_siliconCarbide == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                _resp = scm.Delete(_siliconCarbide);
            }
            return Json(_resp);
        }

       /// <summary>
       /// 修改
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public ActionResult Modify(int id)
        {
            List<SelectListItem> _listItems = new List<SelectListItem>(2);
            _listItems.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems;
            return PartialView(scm.Find(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _breaked = scm.Find(id);
            _breaked.Updated_at = DateTime.Now;
            if (TryUpdateModel(_breaked, new string[] { "Name", "IsDisabled", "Updated_at" }))
            {
                if (scm.IsContains(u => u.Name == _breaked.Name) && form["Name"] != _breaked.Name)
                {
                    _resp.Code = 0;
                    _resp.Message = "名称已存在";
                }
                else
                {
                    _resp = scm.Update(_breaked);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}